This notebook demonstrates how to generate simple embedded animations in Jupyter.
Adapted from: http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-as-interactive-javascript-widgets/
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
fig, ax = plt.subplots()
ax.set_xlim(( 0, 2))
ax.set_ylim((-2, 2))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return (line,)
def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return (line,)
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20,
blit=True)
HTML(anim.to_html5_video())
HTML(anim.to_jshtml())
def make_array(exponent):
x = np.arange(0, 5, 0.01)
y = np.arange(0, 5, 0.01)
xx, yy = np.meshgrid(x, y, sparse=True)
return np.sin(xx**exponent + yy**exponent)
fig, ax = plt.subplots()
im = ax.imshow(make_array(2))
def init():
im.set_data(make_array(0))
return (im,)
def animate(i):
F =50
im.set_data(make_array(i/F))
ax.set_title('i = {}, function_input = {}'.format(i, i/F))
return (im,)
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=50,
blit=True)
HTML(anim.to_html5_video())
HTML(anim.to_jshtml())